﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using System.Data;
using Workflow.Alerts.Utils;
using System.IO;
using System.Xml;
using System.Text;


namespace Workflow.Alerts.Sublayouts
{
    public class InvoiceReportCSV
    {
        public string MunicipalityName { get; set; }

        public void ExportCSV(DataTable data)
        {
            if (data.Rows.Count == 0) return;

            StringBuilder csvContent = new StringBuilder();
            csvContent.AppendLine(csvHeader);

            var alerts = data.Select("rowtype=0");
            for (int no = 0; no < alerts.Length; no++)
            {
                FillAlerts(data, csvContent, (Int32)alerts[no]["alertid"]);
                csvContent.Append(Environment.NewLine);
            }


            var url = String.Format("~/Invoices{0}_{1}.csv", (HttpContext.Current.Session[WorkflowConstants.CurrentUser] as User).MunicipalityId, DateTime.Now.ToString("MMddhh"));
            var path = HttpContext.Current.Server.MapPath(url);

            if (File.Exists(path))
            {
                File.Delete(path);
            }            
            StreamWriter sw = new StreamWriter(path);
            
            sw.Write(csvContent.ToString());
            sw.Flush();
            sw.Close();

            HttpContext.Current.Session["invoiceexport"] = "true";
        }

        private void FillAlerts(DataTable data, StringBuilder csvContent, int alertid)
        {
            double total = 0;

            var alertDtlsTbl = data.Select(string.Format("alertid={0} AND rowtype=0", alertid));
            var alertDtls = alertDtlsTbl.First();
            if (alertDtls != null)
            {
                csvContent.Append(MunicipalityName);
                csvContent.Append(';');
                csvContent.Append(alertDtls["col0"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col1"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col2"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col3"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col4"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col5"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col6"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col7"].ToString());
                csvContent.Append(';');
                csvContent.Append(alertDtls["col8"].ToString());
                csvContent.Append(';');
                csvContent.Append("Leges");
                csvContent.Append(';');                
                csvContent.Append(';');
                csvContent.Append(alertDtls["col9"].ToString());
                csvContent.Append(';');
                csvContent.Append("1");
                csvContent.Append(';');
                csvContent.Append(alertDtls["col9"].ToString());
                csvContent.Append(';');
                

                total = double.Parse(alertDtls["col9"].ToString());
                var csvDtls = new StringBuilder();
                
                var materialsDtlsTbl = data.Select(string.Format("alertid={0} AND rowtype=1", alertid));                
                if (materialsDtlsTbl != null&&materialsDtlsTbl.Length>0)
                {
                    
                    foreach (var materialsDtls in materialsDtlsTbl)
                    {
                        csvDtls.Append(Environment.NewLine);
                        csvDtls.Append(delimiter);

                        csvDtls.Append(materialsDtls["col2"].ToString());
                        csvDtls.Append(';');
                        csvDtls.Append(WorkflowEnums.MaterialsUnits[int.Parse(materialsDtls["col3"].ToString())]);
                        csvDtls.Append(';');
                        csvDtls.Append(materialsDtls["col5"].ToString());
                        csvDtls.Append(';');
                        csvDtls.Append(materialsDtls["col4"].ToString());
                        csvDtls.Append(';');
                        double materialTotal = (double.Parse(materialsDtls["col5"].ToString()) * double.Parse(materialsDtls["col4"].ToString()));
                        csvDtls.Append(materialTotal);
                        total=total + materialTotal;
                        csvDtls.Append(';');
                        csvDtls.Append(';');
                        
                    }
                }
                csvContent.Append(total.ToString("N2"));
                csvContent.Append(';');

                csvContent.Append(csvDtls.ToString());
            }
        }

        string csvHeader = @"Gemeente;Kenmerk factuur;Netwerkbeheerder;Postbus;Postcode;Contactpersoon Voornaam;Contactpersoon achternaam;Code melding;Locatie ;Opgeleverd;Materiaal;Eenheden;Tarief per eenheid;Aantal;Factuurbedrag;Totaal factuurbedrag";
        string delimiter = ";;;;;;;;;;";
    }
}
